/******************************************************************************
 * Author:      671643387
 * Created:     2016/10/14
 *****************************************************************************/

exports.queryResource = function(player, query, resp, onHandled) {
    var user = player.user;
    var name = query.args.name; // 要查询的资源名字
    var num = user.status[name] || 0;
    resp.data = num;
    //LOG("queryResource " + player.uid + " -> " + num);
    onHandled();
};

exports.addResource = function(player, query, resp, onHandled) {
    var user = player.user;
    var targetUid = +query.args.targetUid;
    var num = +query.args.num; // 要加的房卡数量

    if (targetUid == 0) {
        if (!player.addResource('card', num)) {
            resp.code = ErrorCodes.ERROR_ADD_RESOURCE_FAILED;
            resp.desc = findErrString(ErrorCodes.ERROR_ADD_RESOURCE_FAILED);
        }
        LOG(">> addResource " + player.uid + " -> " + player.user.status.card);
        player.save();
        onHandled();
    } else {
        gPlayerMgr.get(targetUid, function(tarPlayer){
            if (!tarPlayer) {
                resp.code = ErrorCodes.ERROR_TARGET_USER_NOT_EXISTS;
                resp.desc = findErrString(ErrorCodes.ERROR_TARGET_USER_NOT_EXISTS);
            } else {

                LOG(">> addResource " + player.uid + " -> " + player.user.status.card + ", " + tarPlayer.uid + " -> " + tarPlayer.user.status.card);

                if (!player.addResource('card', -num)) {
                    // 扣除代理房卡失败
                    resp.code = ErrorCodes.ERROR_ADD_RESOURCE_FAILED;
                    resp.desc = findErrString(ErrorCodes.ERROR_ADD_RESOURCE_FAILED);
                } else {
                    tarPlayer.addResource('card', num);

                    player.save();
                    tarPlayer.save();
                }

                LOG("<< addResource " + player.uid + " -> " + player.user.status.card + ", " + tarPlayer.uid + " -> " + tarPlayer.user.status.card);
            }
            onHandled();
        });
    }
};